package com.rfid.project.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.exception.CannotDelException;
import com.rfid.project.model.ProjectTaskType;
import com.rfid.project.service.ProjectTaskTypeService;
import com.rfid.project.util.StringUtil;

@Controller
public class ProjectTaskTypeController {
	
	private static final Logger logger = LoggerFactory.getLogger(ProjectTaskTypeController.class);
	
	@Autowired
	private ProjectTaskTypeService projectTaskTypeService;
	
	@RequestMapping(value = "/projectTaskTypemgr")
	public String technicalTitlemgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "projectTaskTypemgr";
	}
	
	@RequestMapping(value = "/queryProjectTaskType")
	public String queryProjectTaskType(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<ProjectTaskType> projectTaskTypes = projectTaskTypeService.findAll(name,remark, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", projectTaskTypes.getTotalElements());
		JSONArray models = new JSONArray();
		for (ProjectTaskType projectTaskType : projectTaskTypes) {
			JSONObject mod = new JSONObject();
			wrapData(projectTaskType, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findProjectTaskType")
	public String findProjectTaskType(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			Model model) {
		ProjectTaskType projectTaskType = projectTaskTypeService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(projectTaskType, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(ProjectTaskType projectTaskType, JSONObject mod) {
		mod.put("id", projectTaskType.getId());
		mod.put("name", projectTaskType.getName());
		mod.put("remark", StringUtil.getString(projectTaskType.getRemark()));
	}
	
	@RequestMapping(value = "/editProjectTaskType", method = RequestMethod.POST)
	public String editTechnicalTitle(HttpServletRequest request,
			HttpServletResponse response, @Valid ProjectTaskType projectTaskType,
			Model model, RedirectAttributes redirectAttributes) {
		projectTaskTypeService.saveProjectTaskType(projectTaskType);;

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delProjectTaskType")
	public String delProjectTaskType(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean		flag = true;
		try {
			this.projectTaskTypeService.deleteProjectTaskType(id);;
		} catch (CannotDelException e) {
			flag = false;
			result.put("msg",e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@RequestMapping(value = "/projectTaskTypeChangeOrderNumber")
	public String changeOrderNumber(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id,
			@RequestParam(value = "targetId", defaultValue = "") Long targetId){
		JSONObject result = new JSONObject();
		boolean flag = this.projectTaskTypeService.changeOrderNumber(id, targetId);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
}

